package com.zyd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyd.controller.result.R;
import com.zyd.entity.Employee;
import com.zyd.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api(tags = "Employee接口管理")
@RestController
@RequestMapping("/Employees")
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;

    @ApiOperation("查询所有Employee信息接口")
    @GetMapping
    public R getAll(){
        return new R(true, iEmployeeService.list());
    }

    @ApiOperation("添加Employee信息接口")
    @PostMapping
    public R save(@RequestBody Employee Employee) throws IOException {
//        R r = new R();
//        boolean flag = iEmployeeService.save(Employee);
//        r.setFlag(flag);
        if (Employee.getName().equals("123") ) throw new IOException();
        boolean flag = iEmployeeService.save(Employee);
        return new R(flag, flag ? "添加成功^_^" : "添加失败-_-!");
    }

    @ApiOperation("修改Employee信息接口")
    @PutMapping
    public R update(@RequestBody Employee Employee) throws IOException {
        if (Employee.getName().equals("123") ) throw new IOException();
        boolean flag = iEmployeeService.updateById(Employee);
        return new R(flag, flag ? "修改成功^_^" : "修改失败-_-!");
    }

    @ApiOperation("删除Employee信息接口")
    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(iEmployeeService.removeById(id));
    }

    @ApiOperation("根据id获取Employee信息接口")
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true, iEmployeeService.getById(id));
    }

//    @GetMapping("{currentPage}/{pageSize}")
//    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
//        IPage<Employee> page = iEmployeeService.getPage(currentPage, pageSize);
//        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
//        if( currentPage > page.getPages()){
//            page = iEmployeeService.getPage((int)page.getPages(), pageSize);
//        }
//        return new R(true, page);
//    }

    @ApiOperation("条件分页查询Employee信息接口")
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Employee Employee){
//        System.out.println("参数==>"+Employee);

        IPage<Employee> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>();
        queryWrapper.like(Strings.isNotEmpty(Employee.getName()),"name",Employee.getName());
//        queryWrapper.like(Strings.isNotEmpty(Employee.getDescription()),"description",Employee.getDescription());
//        queryWrapper.like(Strings.isNotEmpty(Employee.getType()),"type",Employee.getType());
        iEmployeeService.page(page, queryWrapper);

//        IPage<Employee> page = iEmployeeService.getPage(currentPage, pageSize,Employee);
//        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
//        if( currentPage > page.getPages()){
//            page = iEmployeeService.getPage((int)page.getPages(), pageSize,Employee);
//        }
        return new R(true, page);
    }

}

















